#include "stm32f10x.h"                  // Device header
#include "Delay.h"

#include "LED.h"
#include "Key.h"
#include "OLED.h"

uint8_t KeyNum = 0;

uint16_t ArrayWrite[] = {0x1234, 0x5678};
uint16_t ArrayRead[2];  

int main(void)
{
	// 初始化
	OLED_Init();
	Key_Init();
	
	// 1. 开启 PWR 和 BKP 时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
	
	// 2. 使能对 BKP 和 RTC 的访问
  PWR_BackupAccessCmd(ENABLE);
	
	OLED_ShowString(1, 1, "W:");
	OLED_ShowString(2, 1, "R:");
	
	while(1)
	{
		KeyNum = Key_GetNum();
		
		
		// 按键1按下, 将 ArrayWrite 数组的内容写入到 BKP 的 DR1 和 DR2 中
		// 并在按键按下的时候, 将 ArrayWrite 数组内容显示到 OLED 上.
		if(KeyNum == 1)
		{
			ArrayWrite[0]++;
			ArrayWrite[1]++;
			
			BKP_WriteBackupRegister(BKP_DR1, ArrayWrite[0]);
			BKP_WriteBackupRegister(BKP_DR2, ArrayWrite[1]);		
			
			OLED_ShowHexNum(1, 3, ArrayWrite[0], 4);
			OLED_ShowHexNum(1, 8, ArrayWrite[1], 4);
		}
		
		// 在 while 循环里面不断刷新显示数据
		ArrayRead[0] = BKP_ReadBackupRegister(BKP_DR1);
		ArrayRead[1] = BKP_ReadBackupRegister(BKP_DR2);
		OLED_ShowHexNum(2, 3, ArrayRead[0], 4);
		OLED_ShowHexNum(2, 8, ArrayRead[1], 4);
	}
}